package cn.edu.zafu.bigdata.lyq;
import java.util.Random;
import java.util.Scanner;

public class GuessGame {
    private int numberToGuess;
    private int maxGuesses;

    public GuessGame() {
    }

    public void startGame(Scanner scanner) {
        System.out.print("请输入你想要设定的最大猜测次数（任意正整数）：");
        maxGuesses = scanner.nextInt();
        Random rand = new Random();
        numberToGuess = rand.nextInt(100) + 1; // 随机数范围1-100

        System.out.println("欢迎来到猜数字游戏！");
        System.out.println("我已经想好了一个1到100之间的数字，你有" + maxGuesses + "次机会猜中它。");

        int guessCount = 0; // 重置猜测次数
        int userGuess = 0; // 初始化userGuess

        while (guessCount < maxGuesses) {
            System.out.print("猜一个1到100之间的数字：");
            userGuess = scanner.nextInt();
            guessCount++;

            if (userGuess == numberToGuess) {
                System.out.println("恭喜你！猜对了！");
                break;
            } else if (userGuess < numberToGuess) {
                System.out.println("太小了！");
            } else if (userGuess > numberToGuess) {
                System.out.println("太大了！");
            }
        }

        if (guessCount >= maxGuesses && numberToGuess != userGuess) {
            System.out.println("很遗憾，你已经用完了所有的猜测次数。");
            System.out.println("正确答案是：" + numberToGuess);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        GuessGame game = new GuessGame();

        while (true) {
            game.startGame(scanner);

            System.out.print("你想再玩一次吗？（输入'y'继续，其他键退出）：");
            String input = scanner.next();
            if (!input.equalsIgnoreCase("y")) {
                break;
            }
        }
        scanner.close();
    }
}